//
//  MSScrollView.m
//  textScrollviewpagecontrol
//
//  Created by ＪＩＹＵＡＮ ＣＡＩ on 12-9-11.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "MSScrollView.h"
#define Max 10

@interface MSScrollView ()<UIScrollViewDelegate> {
    NSArray* _array;
    int pageWith,pageHight;
       
}
@property (strong,nonatomic)UIScrollView *scrollView;
@property (strong,nonatomic)NSMutableArray *slideImages;
@property (strong,nonatomic)UIPageControl *pageControl;
@property (strong,nonatomic)UITapGestureRecognizer * tap;

@end

@implementation MSScrollView
@synthesize scrollView, slideImages;
@synthesize pageControl;
@synthesize tap;
-(id)initWithArray:(NSArray*)arry WithTitle:(NSString *)title WithFram:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _array=arry;
        pageWith=frame.size.width;
        pageHight=frame.size.height;
        
        [self creatScollView];
    }
    return self;
}

-(void)creatScollView {
    
    [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(runTimePage) userInfo:nil repeats:YES];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, pageWith, pageHight)];
    scrollView.bounces = YES;
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    scrollView.userInteractionEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    [self addSubview:scrollView];
    slideImages=[[NSMutableArray alloc] initWithArray:_array];
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(120,pageHight-20,100,18)]; // 初始化mypagecontrol
    [pageControl setCurrentPageIndicatorTintColor:[UIColor whiteColor]];
    [pageControl setPageIndicatorTintColor:[UIColor grayColor]];
    pageControl.numberOfPages = [_array count];
    pageControl.currentPage = 0;
  //  [pageControl addTarget:self action:@selector(turnPage) forControlEvents:UIControlEventValueChanged]; // 触摸mypagecontrol触发change这个方法事件
    [self addSubview:pageControl];
    
    for (int i = 0;i<[slideImages count];i++)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:i]]];
        imageView.frame = CGRectMake((pageWith * i) + pageWith, 0, pageWith, pageHight);
        [scrollView addSubview:imageView]; // 首页是第0页,默认从第1页开始的。所以+320。。。
    }
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:([slideImages count]-1)]]];
    imageView.frame = CGRectMake(0, 0, pageWith, pageHight); // 添加最后1页在首页 循环
    [scrollView addSubview:imageView];
    // 取数组第一张图片 放在最后1页
    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:0]]];
    imageView.frame = CGRectMake((pageWith * ([slideImages count] + 1)) , 0, pageWith, pageHight); // 添加第1页在最后 循环
    [scrollView addSubview:imageView];
    
    [scrollView setContentSize:CGSizeMake(pageWith * ([_array count] + 2), pageHight)]; //  +上第1页和第4页  原理：4-[1-2-3-4]-1
    [scrollView setContentOffset:CGPointMake(0, 0)];
    [self.scrollView scrollRectToVisible:CGRectMake(pageWith,0,pageWith,pageHight) animated:NO];
    
     [self addTapEventOnMyScrollView];
    
}

- (void)addTapEventOnMyScrollView
{
    tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapEvent)];
    [scrollView addGestureRecognizer:tap];
}
-(void)tapEvent{
    [self.delegate tapImage:pageControl.currentPage];
}
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    CGFloat pagewidth = self.scrollView.frame.size.width;
    int page = floor((self.scrollView.contentOffset.x - pagewidth/([slideImages count]+2))/pagewidth)+1;
    page --;  // 默认从第二页开始
    pageControl.currentPage = page;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat pagewidth = self.scrollView.frame.size.width;
    int currentPage = floor((self.scrollView.contentOffset.x - pagewidth/ ([slideImages count]+2)) / pagewidth) + 1;
    //    int currentPage_ = (int)self.scrollView.contentOffset.x/320; // 和上面两行效果一样
    //    NSLog(@"currentPage_==%d",currentPage_);
    if (currentPage==0)
    {
        [self.scrollView scrollRectToVisible:CGRectMake(320 * [slideImages count],0,320,pageHight) animated:NO]; // 序号0 最后1页
    }
    else if (currentPage==([slideImages count]+1))
    {
        [self.scrollView scrollRectToVisible:CGRectMake(320,0,320,pageHight) animated:NO]; // 最后+1,循环第1页
    }
}
- (void)runTimePage
{
    int page = pageControl.currentPage; // 获取当前的page
    page++;
    page = page > [slideImages count]-1 ? 0 : page ;
    pageControl.currentPage = page;
    [self turnPage];
}

// pagecontrol 选择器的方法
- (void)turnPage
{
    int page = pageControl.currentPage; // 获取当前的page
    [self.scrollView scrollRectToVisible:CGRectMake(320*(page+1),0,320,460) animated:NO]; // 触摸pagecontroller那个点点 往后翻一页 +1
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
